#!/bin/bash

# Arguments -
#     remove appName
#     add    appPath
#     check  appName

EXISTS="";

function add {
    /usr/bin/osascript -e "tell application \"System Events\" to make new login item with properties { path: \"$1\", hidden:false } at end";
}

function remove {
    /usr/bin/osascript -e "tell application \"System Events\" to delete login item \"$1\"";
}

function check {
    EXISTS=`/usr/bin/osascript -e "tell application \"System Events\" to get the name of every login item whose name is \"$1\""`;
}

if [[ "$1" == "add" ]]; then
    add $2
    check $2
    if [[ -n "$EXISTS" ]]; then
        echo "True"
        exit 0;
    else
        echo "False"
        exit 1;
    fi
elif [[ "$1" == "remove" ]]; then
    remove $2;
elif [[ "$1" == "check" ]]; then
   check $2
    if [[ -n "$EXISTS" ]]; then
        echo "True"
        exit 0;
    else
        echo "False"
        exit 1;
    fi
fi
